/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.www.csdn.net.stream.nio.t1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
 
/**
 * @description:
 * @author: nxq email: niuxiangqian163@163.com
 * @createDate: 2020/12/28 5:48 下午
 * @updateUser: nxq email: niuxiangqian163@163.com
 * @updateDate: 2020/12/28 5:48 下午
 * @updateRemark:
 * @version: 1.0
 **/
public class Main {
    public static void main(String[] args) {
    quickCopy(new File("/Users/laoniu/a.txt"),new File("/Users/laoniu/b.txt"));
    }
    /**
     * 快速copy
     * @author nxq
     * @param src: 源文件
     * @param target: 目标文件
     * @return void
     */
    public static void quickCopy(File src, File target){
        try(FileInputStream inputStream = new FileInputStream(src);
            FileOutputStream outputStream = new FileOutputStream(target);
            FileChannel inputChannel = inputStream.getChannel(); // 得到源文件通道
            FileChannel outputChannel = outputStream.getChannel()// 得到目标文件通道
        ) {
            //将源文件数据通达连通到目标文件通道进行传输
            inputChannel.transferTo(0,inputChannel.size(),outputChannel);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}